package cn.source.system.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.source.system.domain.RecoveryTable;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.system.domain.DbRecovery;
import cn.source.system.service.IDbRecoveryService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * 数据库恢复记录Controller
 *
 * @author sourcebyte.vip
 * @date 2025-02-26
 */
@RestController
@RequestMapping("/db/recovery")
public class DbRecoveryController extends BaseController {
    @Autowired
    private IDbRecoveryService dbRecoveryService;

    /**
     * 查询数据库恢复记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:recovery:list')")
    @GetMapping("/list")
    public TableDataInfo list(DbRecovery dbRecovery) {
        startPage();
        List<DbRecovery> list = dbRecoveryService.selectDbRecoveryList(dbRecovery);
        return getDataTable(list);
    }

    /**
     * 导出数据库恢复记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:recovery:export')")
    @Log(title = "数据库恢复记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DbRecovery dbRecovery) {
        List<DbRecovery> list = dbRecoveryService.selectDbRecoveryList(dbRecovery);
        ExcelUtil<DbRecovery> util = new ExcelUtil<DbRecovery>(DbRecovery.class);
        util.exportExcel(response, list, "数据库恢复记录数据");
    }

    /**
     * 获取数据库恢复记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:recovery:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dbRecoveryService.selectDbRecoveryById(id));
    }

    /**
     * 新增数据库恢复记录
     */
    @PreAuthorize("@ss.hasPermi('system:recovery:add')")
    @Log(title = "数据库恢复记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DbRecovery dbRecovery) {
        //tables，connName，dbName 必传
        dbRecovery.setStartTime(new Date());
        dbRecoveryService.insertDbRecovery(dbRecovery);
        return AjaxResult.success(dbRecovery.getId());//返回记录的id
    }

    /**
     * 修改数据库恢复记录
     */
    @PreAuthorize("@ss.hasPermi('system:recovery:edit')")
    @Log(title = "数据库恢复记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DbRecovery dbRecovery) {
        return toAjax(dbRecoveryService.updateDbRecovery(dbRecovery));
    }

    /**
     * 删除数据库恢复记录
     */
    @PreAuthorize("@ss.hasPermi('system:recovery:remove')")
    @Log(title = "数据库恢复记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dbRecoveryService.deleteDbRecoveryByIds(ids));
    }


    /**
     * 下载日志
     */
    @Log(title = "下载日志", businessType = BusinessType.DELETE)
    @PostMapping("/downloadLog")
    public void downloadLog(Integer recoveryId, HttpServletResponse response) {
        dbRecoveryService.downloadLog(recoveryId, response);
    }

    /**
     * 下载压缩文件
     *
     * @param recoveryTables
     * @param response
     */
    @Log(title = "下载备份文件", businessType = BusinessType.DELETE)
    @PostMapping("/downloadBackupFile")
    public void downloadBackupFile(String paths,
                                   HttpServletResponse response) {
        List<String> pathList = Arrays.asList(paths.split("#"));
        List<RecoveryTable> recoveryTables = pathList.stream().map(item -> {
                    RecoveryTable recoveryTable = new RecoveryTable();
                    recoveryTable.setFileAbsolutePath(item);
                    return recoveryTable;
                }
        ).collect(Collectors.toList());
        dbRecoveryService.downloadBackupFile(recoveryTables, response);
    }


}
